<template>
  <div class="app-container app-container-table">
    <one-table-template
      v-if="initTemplate"
      ref="multipleTable"
      :dynamicButtons="tableButtons"
      :dynamicComponents="tableComponents"
      :dynamicTableCols="tableCols"
      :dynamic-api-config="apiConfig"
      :dynamicFormFields="formField"
      :dynamicIsShowSelect="false"
      :dynamicIsColumnDrop="true"
    >
      <template slot="controlBtn" slot-scope="scope">
        <el-link
          type="primary"
          class="el-icon-edit"
          :underline="false"
          @click="edit(scope.row.index)"
          style="font-size:12px; margin-right:10px;"
          v-if="scope.row.AREA_TYPE=== '00' "
        >编辑</el-link>
        <!-- <el-link
          type="primary"
          class="el-icon-printer"
          :underline="false"
          @click="printShow(scope.row)"
          style="font-size:12px;"
        >打印库位标签</el-link> -->
      </template>
    </one-table-template>
    <edit
      :key="editPopupsKey"
      :dynamic-edit-row-data="editRowData"
      :popups-visible="editPopupsVisible"
      :popups-state="editPopupsState"
      :is-show-chooes="isShowChooes"
      :warehouseArr="warehouseArr"
      @close="close"
    />
    <!-- <print @colse="colsePrint" :data="printData" v-if="isShow"></print> -->
    <print v-if="isShow" :rowData="rowData" @detailClose="detailClose" :isShow="isShow" />
  </div>
</template>

<script>
import OneTableTemplate from "@/components/wms/templates/oneTable";
import { basedataApi } from "@/api/wms/basedata";
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import requesta from "../../../../utils/request.js";
export default {
  components: {
    OneTableTemplate,
    Edit: () => import("./edit"),
    Print: () => import("./print")
  },
  mixins: [oneTableWithViewTemplateMixins],
  watch: {
    "formField.WAREHOUSE_IDS": {
      handler: function() {
        var wArr = [];
        if (this.formField.WAREHOUSE_IDS == "") {
          this.tableComponents[0].options.map(item => {
            wArr.push(item.value);
          });
        }
        var WAREHOUSE_IDS_NEW = "";
        WAREHOUSE_IDS_NEW = wArr.join(",");
        if (WAREHOUSE_IDS_NEW != "") {
          this.formField.WAREHOUSE_IDS_NEW = WAREHOUSE_IDS_NEW;
        } else {
          this.formField.WAREHOUSE_IDS_NEW = "";
        }
      }
    },
    watch: {
      $route(val) {
        if (
          this.formField.pageId !== "" &&
          this.$route.query.pageId !== undefined &&
          this.$route.query.pageId !== this.formField.pageId
        ) {
          this.formField.pageId = this.$route.query.pageId;
          this.formField.pageName = this.$route.query.pageName;
          this.queryTable();
        }
      }
    }
  },
  data() {
    return {
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          name: "search",
          position: "right",
          clickEvent: () => this.queryTable(1),
          text: this.$t("sys.button.query") /*查询*/
        },
        {
          compKey: "btnKey4",
          type: "",
          size: "small",
          name: "reset",
          position: "right",
          clickEvent: () => this.reset(),
          text: this.$t("sys.button.reset") /*重置*/
        },
        {
          compKey: "btnKey3",
          type: "",
          size: "small",
          name: "add ",
          position: "left",
          clickEvent: () => this.add(),
          text: "新增" /*导出*/
        },
        {
          compKey: "btnKey4",
          type: "",
          size: "small",
          name: "add ",
          position: "left",
          clickEvent: () => this.export(),
          text: "导出" /*导出*/
        }
      ],
      tableComponents: [
        {
          compKey: "compKey1",
          labelName: "仓库名称",
          codeField: "WAREHOUSE_IDS",
          component: () => import("@/components/org/dropdownList/dropdownList"),
          options: [],
          type: "dropdownList",
          isMust: true,
          isMul: true
        },
        {
          compKey: "compKey2",
          labelName: "库区编号",
          codeField: "AREA_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey3",
          labelName: "库区名称",
          codeField: "AREA_NAME",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        }
      ],
      tableCols: this.getCols(),
      //表单查询数据
      formField: {
        WAREHOUSE_IDS: "",
        AREA_CODE: "",
        AREA_NAME: ""
      },
      isShowChooes: false,
      apiConfig: basedataApi.queryarea,
      warehouseArr: [],
      isShow: false,
      rowData: {},
      // 控制初始化页面
      initTemplate: false
    };
  },
  created() {
    this.dropdownList();
  },
  mounted() {},
  methods: {
    printShow(row) {
      this.rowData = row;
      this.isShow = true;
    },
    detailClose() {
      this.isShow = false;
    },
    add() {
      // this.isShowChooes = true;
      this.showEdit("add");
    },
    dropdownList() {
      requesta({
        url: "/mp/wm/vl/basedata/warehouseuser/queryUserByWHBox.do",
        method: "POST",
        headers: {
          "Cache-Control": "no-cache, no-store,max-age=0, must-revalidate",
          Pragma: "no-cache",
          Expires: "0",
          "Content-Type": "application/json;charset=UTF-8"
        },
        data: {}
      }).then(res => {
        if (res.listRow && res.listRow.length > 0) {
          res.listRow.map(item => {
            this.tableComponents[0].options.push({
              label: item.WAREHOUSE_NAME,
              value: item.WAREHOUSE_ID
            });
          });
          this.warehouseArr = this.tableComponents[0].options;
          this.formField.WAREHOUSE_IDS = this.tableComponents[0].options[0].value;
        }
        this.initTemplate = true;
        this.$nextTick(() => {
          this.queryTable();
        });
      });
    },
    export() {
      const exportLoading = this.$loading({
        lock: true,
        text: "正在导出...",
        // spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
        fullscreen: true
      });
      requesta({
        url: "/mp/wm/vl/basedata/area/exportarea.do",
        method: "POST",
        headers: {
          "Content-Type": "application/json;charset=UTF-8"
        },
        responseType: "blob",
        data: this.formField
      })
        .then(response => {
          exportLoading.close();
          let blob = new Blob([response], { type: "application/vnd.ms-excel" }); // 这里表示xmlx类型
          let downloadElement = document.createElement("a");
          let href = window.URL.createObjectURL(blob); // 创建下载的链接
          downloadElement.href = href;
          downloadElement.download = "库区管理.xls"; // 下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); // 点击下载
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象
        })
        .catch(err => {
          exportLoading.close();
          console.info(err);
        });
    },
    getCols() {
      var cols = [
        {
          prop: "controlBtn",
          label: this.$t("sys.content.operate") /*操作*/,
          codeField: "controlBtn",
          width: 160,
          align: "center",
          isSlot: true
          // comps: [
          //   {
          //     compKey: "propKey0",
          //     align: "center",
          //     labelName: this.$t("sys.button.edit") /*编辑*/,
          //     codeField: "editControlBtn",
          //     clickEvent: this.edit,
          //     component: () => import("@/components/org/linkButton")
          //   },
          //   {
          //     compKey: "propKey1",
          //     align: "center",
          //     labelName: " 打印库位标签" /*编辑*/,
          //     codeField: "editControlBtn",
          //     clickEvent: this.printShow,
          //     component: () => import("@/components/org/linkButton")
          //   }
          // ]
        }
      ];
      cols = cols.concat([
        {
          prop: "WAREHOUSE_NAME",
          label: "仓库名称",
          width: null,
          align: "center"
        },
        {
          prop: "AREA_CODE",
          label: "库区编号",
          width: null,
          align: "center"
        },
        {
          prop: "AREA_NAME",
          label: "库区名称",
          width: null,
          align: "center"
        },
        {
          prop: "AREA_TYPE_NAME",
          label: "库区类型",
          width: null,
          align: "center"
        },
        {
          prop: "AREA_ATTR_NAME",
          label: "库区属性",
          width: null,
          align: "center"
        },
        {
          prop: "CREATED_DATE",
          label: "创建时间",
          width: null,
          align: "center"
        },
        {
          prop: "CREATOR",
          label: "创建人",
          width: null,
          align: "center"
        },
        {
          prop: "IS_ENABLE_STR",
          label: "状态",
          width: null,
          align: "center"
        }
      ]);
      return cols;
    }
  }
};
</script>
